home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 76 < prev    next >
Internet Message Format  |  1996-08-06  |  4KB

  1. Path: lyra.csx.cam.ac.uk!nmm1
  2. From: nmm1@cus.cam.ac.uk (Nick Maclaren)
  3. Newsgroups: comp.std.c,comp.arch.arithmetic,sci.math.num-analysis
  4. Subject: Why is <float.h>? [was Re: sizeof(1L) in preprocesor ...]
  5. Date: 10 Jan 1996 23:19:03 GMT
  6. Organization: University of Cambridge, England
  7. Distribution: inet
  8. Message-ID: <4d1hh7$kd@lyra.csx.cam.ac.uk>
  9. References: <sc3f9vb6gk.fsf@lns101.lns.cornell.edu> <4ctk66$57j@lyra.csx.cam.ac.uk> <KANZE.96Jan9135752@slsvewt.lts.sel.alcatel.de> <4ctpkv$82v@lyra.csx.cam.ac.uk>
  10. NNTP-Posting-Host: apus.cus.cam.ac.uk
  11.  
  12. I was asked (by Email) to explain why the values in <float.h> are
  13. expressions that evaluate to rvalues, and why this is imbecilic.
  14. Please ignore this if you know, and flame me if you feel the urge!
  15.  
  16.  
  17. The Intel 8087 and similar chips have operations that allow a program
  18. to change the rounding modes, exception handling and even effective
  19. mantissa length.  They do not have an operation to change the radix,
  20. as was once proposed for the IBM 370 architecture (but never, as far
  21. as I know, implemented).
  22.  
  23. During the design of the ANSI standard, the 'PC camp' insisted that
  24. the values in <float.h> had to be general rvalues to allow for this.
  25. The traditionalists and numerical analysts argued for the values to
  26. be preprocessor constants, as in all the other headers, but lost.  I
  27. was a very minor member of the latter camp.
  28.  
  29. I know of no C run-time system that has taken advantage of this aspect
  30. of the standard to support dynamically reconfigurable arithmetic; it
  31. would be possible to implement, but there are very, very few people
  32. capable of it.  And that is even before you allow for the possibility
  33. of the arithmetic properties being changed in an asynchronous signal
  34. handler :-)
  35.  
  36.  
  37. So why is this imbecilic?
  38.  
  39. Numerical analysts use values of the sort that are found in <float.h>
  40. for many purposes, including:
  41.  
  42.     1) Switching to alternate algorithms for special functions.
  43.     2) Avoiding overflow, underflow and rounding error.
  44.     3) Selecting appropriate precisions for conversion on I/O.
  45.     4) Varying the algorithm in the rare cases where it matters.
  46.  
  47. Now, (1) needs the values to be preprocessor constants for efficiency.
  48. Remember that such people are the very people who write the functions
  49. called via <math.h>, and we like to be able to write code that compiles
  50. automatically and correctly even on machines that we have never heard
  51. of!  But to do this efficiently, we have to define our OWN <float.h>,
  52. which isn't portable!
  53.  
  54. For categories (2)-(4), efficiency is not crucial.  But it is rare
  55. that the values need be exact, either.  And any numerical analyst
  56. worth his salt is quite capable of calculating the values himself,
  57. and so doesn't need <float.h> in the first place.  Anyway, we don't
  58. trust run-time system authors, for very good reasons.  You wouldn't
  59. believe how many systems have the values in <float.h> wrong!
  60.  
  61. So <float.h> is, at best, a minor convenience to numerical analysts.
  62.  
  63. End users who are not numerical analysts need the values in <float.h>
  64. for reasons (2)-(4) and similar ones.  But they don't need the detail
  65. that it provides - they need only an indication of the accuracy, an
  66. indication of the maximum value, and some flags indicating whether
  67. underflow/overflow/etc. are handled in a sensible fashion (which
  68. aren't specified in <float.h>, anyway).
  69.  
  70. So <float.h> is vastly more complex than end users need.
  71.  
  72.  
  73. I speak as someone who has written functions in <math.h>, who has
  74. also written a C run-time system, who writes software that uses
  75. <float.h> and who writes codes that tests <float.h>.  And I still
  76. haven't found any justification for its existence in its current
  77. form.
  78.  
  79. So, am I being unreasonable when I am rude about it?
  80.  
  81.  
  82. Nick Maclaren,
  83. University of Cambridge Computer Laboratory,
  84. New Museums Site, Pembroke Street, Cambridge CB2 3QG, England.
  85. Email:  nmm1@cam.ac.uk
  86. Tel.:  +44 1223 334761    Fax:  +44 1223 334679
  87.